<?php

use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;

function hiddenAccount($account) {
    return substr($account,0,1) . '****' . substr($account,0,-1);
}

/**
 * 获取开服时间
 * @param $server
 * @return string
 */
function getOpenServerTime ($server) {
    $nowHour = (int)date('H');
    if ($nowHour < 7) {
        return '【※通宵推荐※】';
    }
    if ($server->is_every_day == 1 || $server->is_all_day == 1) {
        $curMin = (int)date('i');
        $curHours = (int)date('H');
        if ($curMin == 0) {
            $curHours -= 1;
            $curMin = 30;
        } else {
            $curMin = $curMin <= 30 ? '00' : '30';
        }
        $curHours = $curHours < 10 ? ('0'.$curHours) : $curHours;
        return '<span style="color: red">'.sprintf('今日%s:%s', $curHours, $curMin).'</span>';
    }
    return '<span style="color: red">今日'.date('H:i',strtotime($server->open_time)).'</span>';
}

function echoGetLogErr ($log) {
    if ($log->status == 3) {
        return '<span style="color: red">异常(' . $log->msg .')</span>';
    }
    return ($log->status == 1 ? '待充值(' : '已充值(') . $log->money . ')';
}

/**
 * 是否手机
 * @param $mobile
 * @return bool
 */
function isMobile ($mobile) {
    return preg_match("/^1[3456789]{1}\d{9}$/",$mobile) ? true : false;
}

function isAfter16($row) {
    $today = date('Y-m-d', time());
    if ($today.date(' H:i:s', strtotime($row->create_time)) > $today.' 16:30:00' && $row->date == '昨日') {
        return $row->game_url;
    }
    return $row->game_server;
}

function successResp($data) {
    return response()->json(['message' => '操作成功', 'success' => 'true', 'data' => $data]);
}

function errorResp($msg) {
    return response()->json(['message' => $msg, 'success' => 'false', 'data' => []]);
}

function getCurDomain() {
    $url  = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
    $url .= "://" . $_SERVER['HTTP_HOST'];
    return $url;
}


function isSuperAdmin() {
    return Illuminate\Support\Facades\Auth::user()->role === 0;
}

function sendRechargeWxTemplateMsg($openid, $account, $gameName, $money, $balance) {
    $account_key = 'account_1'; // 选择你需要的公众号
    $config = config('wechat.public_accounts.' . $account_key);
    // $config = config('wechat');
    $app = Factory::officialAccount($config);
    $ret = $app->template_message->send([
        'touser' => $openid,
        'template_id' => 'sXHXqEVNX2dwvSid4nc6yhXBsu9O6ZiZHJMaBRPJUSc',
        'url' => 'http://h5.x5q.vip/login.php/',
        'data' => [
            'character_string5' => $account,
            'phrase6' => $gameName,
            // 'thing10' =>  '双授权网站-查看详情可进手机站',
            'amount2' => $money,
            // 'amount3' => $balance,
        ],
    ]);
    Log::info($ret);
}
// function sendRechargeWxTemplateMsg2($openid, $gameName, $money) {
//     $config = config('wechat');
//     $time=now();
//     $app = Factory::officialAccount($config);
//     $ret = $app->template_message->send([
//         'touser' => $openid,
//         'template_id' => 'iyWQ9cStkHjMPGSR7y88lrxWHS9r-kErmOYaN74uNsI',
//         'url' => 'http://h5.x5q.vip/login.php/',
//         'data' => [
//             'thing2' =>'您有一个专属福利请到'. $gameName.'去兑换',
//             'amount3' => $money,
//             'time5' =>  $time,
//             'thing4' => '联系QQ客服2881951587',
//             'thing7' => '2024wf会员平台',
//         ],
//     ]);
//     Log::info($ret);
// }

function sendRechargeWxTemplateMsg2($openid, $gameName, $money) {
    try {
        $account_key = 'account_1'; // 选择你需要的公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);

        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'iyWQ9cStkHjMPGSR7y88lrxWHS9r-kErmOYaN74uNsI',
            // 'url' => 'http://h5.x5q.vip/login.php/',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                // 'thing2' =>'您被系统抽中专属福利请到'. $gameName.'去兑换',
                'thing2' =>'恭喜抽中专属福利，速领'.$gameName.'奖励',
                'amount3' => $money,
                'time5' =>  $time,
                'thing4' => '联系QQ客服2881951587',
                'thing7' => 'wf100.cn抽奖平台站',
            ],
        ]);

        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}
function sendRechargeWxTemplateMsg9($openid,$money) {
    try {
        $account_key = 'account_1'; // 选择你需要的公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);

        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'Es0PCKiNY8czXqAJVFmTIw1ryBPcpMf73AGduigAx48',
            // 'url' => 'http://h5.x5q.vip/login.php/',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                'amount6' =>$money,
                'thing4' => '长时间不使用易被冻结请联系平台客服',
                'time8' =>  $time,
                'thing9' => 'wf100.cn抽奖平台站',
            ],
        ]);

        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}

function sendRechargeWxTemplateMsg10($openid,$money) {
    try {
        $account_key = 'account_1'; // 选择你需要的公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);

        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'visHar8Fo2q6i3oNyNOGC5MBbPJhUm4UsNj1goLT2K0',
            // 'url' => 'http://h5.x5q.vip/login.php/',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                'amount3' =>$money,
                'thing10' => '邀请您使用该'.$money.'积分抽奖得乐游币',
                'time4' =>  $time,
                'thing1' => 'wf100.cn抽奖平台站',
            ],
        ]);

        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}
function sendRechargeWxTemplateMsg3($openid, $gameName) {
    try {
        $account_key = 'l3y2'; // hz乐享游公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now()->format('Y-m-d'); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //  var_dump($app);die;
        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'NaDADHJj2PYXfIQqzcCOQnQlojK0-ge8WLCLZZljwHo',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                'thing2' =>'wf100.cn抽奖会员站',
                'time10' =>  $time,
                'thing16' => '您被系统抽中福利请点击详情咨询',
            ],
        ]);
        // var_dump($result);
        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}

function sendRechargeWxTemplateMsg5($openid, $gameName) {
    try {
        $account_key = 'l3y2'; // hz乐享游公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //  var_dump($app);die;
        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'v1Eyfb21N6mqD7ZjZbQBlkbezmikDtWye9PZtZZT1Xs',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                // 'thing4' =>'您被系统抽中专属福利请到'.$gameName.'兑换',
                 'thing4' =>'恭喜抽中专属福利，速领'.$gameName.'奖励',
                'time3' =>  $time,
                'amount10' => 5000,
                'thing5' => '可免费抽奖及拿沙奖',
                'thing8' => 'wf100.cn抽奖会员站',
                
            ],
        ]);
        // var_dump($result);
        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}



function sendRechargeWxTemplateMsg4($openid, $gameName,$gamenum) {
    try {
        $account_key = 'l3y1'; // 原鸿高商贸商行公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now()->format('Y-m-d'); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //  var_dump($app);die;
        // 发送模板消息
        $result = $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'vYzkDcc-tdKzVaufY_xICVc4Rvr6fSd0Ak83OdMis3I',
            'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
            'data' => [
                'character_string2' =>$gamenum,
                'thing6' => $gameName.'首次攻沙20点集合',
                'time4' => $time,
                'thing5' => 'wf100.cn会员抽奖平台',
            ],
        ]);
        // var_dump($result);
        // 检查发送结果
         // 检查微信接口返回的结果
        if ($result['errcode'] == 0) {
          // Log::info('Message sent successfully to user with openid: ' . $openid);
            return true;
        } else {
          // Log::warning('Failed to send message to user with openid: ' . $openid . '. Error: ' . $result['errmsg']);
            return false;
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}

function sendRechargeWxTemplateMsg6($openid) {
    try {
        $account_key = 'account_1'; // 网站公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //   var_dump($openid);die;
        // 发送模板消息
        if($openid){
            $result = $app->template_message->send([
                'touser' => $openid,
                'template_id' => 'K4TK-YVZWn8K-XRsMoLd2IuptPH7sOUnRiDhztCqMSk',
                'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
                'data' => [
                    'time1' => $time,
                    'thing4' => 'wf100.cn站可抽中乐游币',
                    'thing3' => 'wf100.cn抽奖平台站',
                ],
            ]);
            if ($result['errcode'] == 0) {
         
            return true;
            } else {
            
                return false;
            }
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}

function sendRechargeWxTemplateMsg7($openid) {
    try {
        $account_key = 'account_1'; // 网站公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        // $time = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //   var_dump($openid);die;
        // 发送模板消息
        if($openid){
            $result = $app->template_message->send([
                'touser' => $openid,
                'template_id' => 'WNcI3CBCEWE-xDse3diYr24SXcIgd8GRYSJmBVJRotM',
                'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
                'data' => [
                    'time5' => $time,
                    'thing6' => '您被系统抽中<天命三国>版本福利',
                    'thing9' => 'wf100.cn会员抽奖平台',
                ],
            ]);
            if ($result['errcode'] == 0) {
            return true;
            } else {
                return false;
            }
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}
function sendRechargeWxTemplateMsg8($openid) {
    try {
        $account_key = 'account_1'; // 网站公众号
        $config = config('wechat.public_accounts.' . $account_key);
        // $config = config('wechat');
        $time = now(); // 获取当前时间的字符串格式
        $time1 = now()->toDateTimeString(); // 获取当前时间的字符串格式
        $app = Factory::officialAccount($config);
        
        //   var_dump($openid);die;
        // 发送模板消息
        if($openid){
            $result = $app->template_message->send([
                'touser' => $openid,
                'template_id' => '5cA-vrl69AOck-DKsJJ6dveWAD_P2KfyBIRDmgB82_Y',
                'url' => 'https://work.weixin.qq.com/kfid/kfc725206fcdc35a89e?enc_scene=ENC9wSUYGLisLvy1KUCVV11qCx6pW6dAzDgas3AoHdGk2sehjMfDQYnqk2ayvcQgicwRy',
                'data' => [
                    // 'thing3' => '<诛仙战神>首沙有现金奖励请积极报名参加',
                    // 'thing3' => '恭喜您已获攻沙资格，速联客服领取专属奖励',
                    'thing3' => '九剑专属喊您赢现金速进游戏参与攻沙',
                    'thing4' => 'wf100.cn抽奖平台',
                    'time5' => '2024年09月30日 19:40',
                    'time6' => '2024年09月30日 20:00',
                    'time8' => '2024年09月30日 22:00',
                ],
            ]);
            if ($result['errcode'] == 0) {
            return true;
            } else {
                return false;
            }
        }
    } 
    catch (\Exception $e) {
        Log::error('Exception occurred while sending template message: ' . $e->getMessage());
        return false;
    }
}
//     function sendRechargeWxTemplateMsg2($openid, $gameName, $money) {
//     $config = config('wechat');
//     $time = now();
//     // var_dump($unionid);die; 
//     $app = Factory::officialAccount($config);
//     // var_dump($app);die; 
//     // Prepare the template message data
//     $data = [
//         'touser' => $openid,
//         'template_id' => 'iyWQ9cStkHjMPGSR7y88lrxWHS9r-kErmOYaN74uNsI',
//         'url' => 'http://h5.x5q.vip/login.php/',
//         'data' => [
//             'thing2' => ['value' => $gameName . '您有一个专属红包待领取'],
//             'amount3' => ['value' => $money],
//             'time5' => ['value' => $time],
//             'thing4' => ['value' => '联系QQ客服2881951587'],
//             'thing7' => ['value' => '2024wf会员平台'],
//         ],
//     ];
    
//     try {
//         // Send the message
//         $ret = $app->template_message->send($data);
        
//         // Log the response from WeChat API
//         Log::info('WeChat Template Message Response:', ['response' => $ret]);
        
//         // Check if the message was sent successfully
//         if ($ret['errcode'] == 0) {
//             Log::info('Template message sent successfully to ' . $openid);
//              return true;
//         } else {
//             Log::error('Failed to send template message: ' . $ret['errmsg']);
//             return false;
//         }
//     } catch (\Exception $e) {
//         // Log any exceptions that occur during the API call
//         Log::error('Exception occurred while sending template message: ' . $e->getMessage());
//         return false;
//     }
// }


function getRandVersionType ($id) {
    $vl = [
        '烈火骑士','星空传奇','百战沙场','屠龙战神'
    ];
    return $vl[$id%4];
}
